package cn.zzdt4j.config.service;

import cn.hutool.core.collection.CollectionUtil;
import cn.zzdt4j.common.extension.design.AbstractSubjectCenter;
import cn.zzdt4j.common.extension.design.Observer;
import cn.zzdt4j.common.extension.design.ObserverMessage;
import cn.zzdt4j.common.toolkit.Joiner;
import cn.zzdt4j.common.toolkit.MapUtil;
import cn.zzdt4j.config.model.CacheItem;
import cn.zzdt4j.config.service.biz.ConfigService;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import static cn.zzdt4j.common.constant.Constants.*;
import static cn.zzdt4j.common.constant.MagicNumberConstants.INDEX_3;

/**
 * The type Config cache service.
 *
 * @author by <a href="mailto:ligang941012@gmail.com">gang.Li</a>
 * @since 2023 /11/6 20:38
 */
@Slf4j
public class ConfigCacheService {
    private static ConfigService configService;

    static {
        AbstractSubjectCenter.register(AbstractSubjectCenter.SubjectType.CLEAR_CONFIG_CACHE, new ClearConfigCache());
    }

    /**
     * key: message-produce+dynamic-threadpool-example+prescription+192.168.20.227:8088_xxx
     * val:
     * key: 192.168.20.227:8088_xxx
     * val:  {@link CacheItem}
     */
    private static final ConcurrentHashMap<String, Map<String, CacheItem>> CLIENT_CONFIG_CACHE = new ConcurrentHashMap<>();

    /**
     * Gets content.
     *
     * @param identification the identification
     * @return the content
     */
    public static Map<String, CacheItem> getContent(String identification) {
        final List<String> identificationList = MapUtil.parseMapForFilter(CLIENT_CONFIG_CACHE, identification);
        Map<String, CacheItem> returnStrCacheItemMap = new HashMap<>();
        identificationList.forEach(each -> returnStrCacheItemMap.putAll(CLIENT_CONFIG_CACHE.get(each)));
        return returnStrCacheItemMap;
    }

    public static List<String> getIdentifyList(String tenantId, String itemId, String threadPoolId) {
        List<String> identifyList = null;
        String buildKey = Joiner.on(GROUP_KEY_DELIMITER).join(CollectionUtil.newArrayList(threadPoolId, itemId, tenantId));
        List<String> keys = MapUtil.parseMapForFilter(CLIENT_CONFIG_CACHE, buildKey);
        if (CollectionUtil.isNotEmpty(keys)) {
            identifyList = new ArrayList<>(keys.size());
            for (String each : keys) {
                String[] keyArray = each.split(GROUP_KEY_DELIMITER_TRANSLATION);
                if (keyArray.length > 2) {
                    identifyList.add(keyArray[INDEX_3]);
                }
            }
        }
        return identifyList;
    }

    private static synchronized void coarseRemove(String coarseMessage) {
        // fuzzy search
        final List<String> identificationList = MapUtil.parseMapForFilter(CLIENT_CONFIG_CACHE, coarseMessage);
    }

    /**
     * The type Clear config cache.
     */
    static class ClearConfigCache implements Observer<String> {
        @Override
        public void accept(ObserverMessage<String> observerMessage) {
            log.info("Clean up the configuration cache. key: {}", observerMessage.message());
            coarseRemove(observerMessage.message());
        }

    }
}
